﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ExpertMIS.Brokers;
using System.Collections;
using ExpertMIS.Entities;
using ExpertMIS.UserControl;
using System.Web.UI.HtmlControls;

namespace ExpertMIS.Forums
{
    public partial class DomainIntroduce : System.Web.UI.Page
    {
        private DomainBroker _domainBroker = new DomainBroker();
        private QuestionBroker _questionBroker = new QuestionBroker();
        private ExpertBroker _expertBroker = new ExpertBroker();
        private string _currentDomainOID = string.Empty;

        private void RefreshDomains()
        {
            dlDomains.DataSource = _domainBroker.GetAllDomains();
            dlDomains.DataBind();
        }

        private void RefreshRecommendExperts(string DomainOID)
        {
            ExpertBroker broker = new ExpertBroker();

            dlRecommendExperts.DataSource = broker.GetDomainRecommandExperts(DomainOID, 11);
            dlRecommendExperts.DataBind();
        }

        private void RefreshDomainFAQ(string DomainOID)
        {
            dlFAQs.DataSource = _questionBroker.GetDomainFAQs(DomainOID, 16);
            dlFAQs.DataBind();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(Request["oid"]))
            {
                _currentDomainOID = Request["oid"];
                RefreshDomains();
                RefreshRecommendExperts(Request["oid"]);
                RefreshDomainFAQ(Request["oid"]);
            }
        }

        protected void dlRecommendExperts_ItemDataBound(object sender, DataListItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                ExpertEntity entity = e.Item.DataItem as ExpertEntity;
                ucExpertShort ucShort = e.Item.FindControl("ucExpertShort1") as ucExpertShort;
                if (entity != null && ucShort != null)
                {
                    ucShort.Expert = entity;
                }
            }
        }

        protected void dlFAQs_ItemDataBound(object sender, DataListItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
            {
                QuestionEntity question = e.Item.DataItem as QuestionEntity;
                if (null == question) return;

                HyperLink hlExpert = e.Item.FindControl("hlExpert") as HyperLink;
                if (hlExpert != null)
                {
                    ExpertEntity expert = _expertBroker.GetExpertEntityByUserID(question.UserOID);
                    if (expert != null)
                    {
                        hlExpert.Text = expert.Name;
                        hlExpert.NavigateUrl = string.Format("{0}{1}", ResolveUrl("~/Forums/ExpertView.aspx?oid="), expert.OID);
                    }
                }

                Label lblDate = e.Item.FindControl("lblDate") as Label;
                if (lblDate != null)
                {
                    lblDate.Text = string.Format("{0:yy-MM-dd}", question.UpdateDate);
                }
            }
        }

        protected void dlDomains_ItemDataBound(object sender, DataListItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                DomainEntity domain = e.Item.DataItem as DomainEntity;
                HyperLink hlDomain = e.Item.FindControl("hlDomain") as HyperLink;
                //HtmlAnchor anchor = e.Item.FindControl("hrefDomain") as HtmlAnchor;

                //if (domain != null && hlDomain != null && anchor != null)
                if (domain != null && hlDomain != null)

                {
                    hlDomain.Text = domain.DomainName;

                    if (domain.OID == _currentDomainOID)
                    {
                        hlDomain.Enabled = false;
                        hlDomain.Font.Bold = true;
                    }
                    else
                    {
                        hlDomain.NavigateUrl = "DomainIntroduce.aspx?oid=" + domain.OID;
                        //anchor.HRef = "DomainIntroduce.aspx?oid=" + domain.OID;
                    }
                }

                Image imgDomainIcon = e.Item.FindControl("imgDomainIcon") as Image;
                if (domain != null && imgDomainIcon != null)
                {
                    string imgUrl = ExpertMIS.Codes.Utility.GetEnvironmentValue(domain.OID + ".icon");
                    if (!string.IsNullOrEmpty(imgUrl))
                    {
                        imgDomainIcon.ImageUrl = imgUrl;
                    }
                }
            }
        }
    }
}
